package cn.lagou

object Question1 {
  def main(args: Array[String]): Unit = {
    val price = 100
    val unitPrice = 2
    // 能购买到的啤酒数量
    val num = price / unitPrice

    println(num + calcBeers(num, num, 0))
  }

  /**
   *
   * @param empty     空瓶数量
   * @param bottleCap 瓶盖数量
   * @param bottleNum 啤酒瓶数量
   * @return
   */
  @scala.annotation.tailrec
  def calcBeers(empty: Int, bottleCap: Int, bottleNum: Int): Int = {
    // 空瓶兑换的啤酒
    val exchangeEmpty = empty / 3
    // 剩余的空瓶数量
    val leftEmpty = empty % 3
    // 瓶盖兑换的啤酒
    val exchangeBottleCap = bottleCap / 5
    // 剩余的瓶盖数量
    val leftBottleCap = bottleCap % 5

    if (empty < 3 && bottleCap < 5) {
      bottleNum
    } else {
      calcBeers(exchangeEmpty + exchangeBottleCap + leftEmpty, exchangeEmpty + exchangeBottleCap + leftBottleCap, exchangeEmpty + exchangeBottleCap + bottleNum)
    }
  }
}